﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using System.Collections;
using System.Net.Mail;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Text;


namespace logicunivesityinventorystore.Department
{
    public partial class SendOrder : System.Web.UI.Page
    {
        OrderFacade ofc = new OrderFacade();
        EmployeeFacade efc = new EmployeeFacade();
        SuppliersFacade sup = new SuppliersFacade();
        //set here for the employee id from session
        string empid;

        protected void Page_Load(object sender, EventArgs e)
        {
            empid = User.Identity.Name;
            if (!IsPostBack)
            {
                GridView1.DataBind();
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count == 0)
            {
                ScriptManager.RegisterStartupScript(this, typeof(string), "Error", "alert('No Purchase Order for this supplier!');window.location.href = 'SendOrder.aspx'", true);
            }
            else
            {
                btnSend.Visible = true;
                lblPurchase.Visible = true;
                ddlNos.Visible = true;
                lblMsg.Visible = false;
                GridView1.Visible = true;
            }
        }

        protected void ddlNos_SelectedIndexChanged(object sender, EventArgs e)
        {       
          
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            ////update the status to Purchased
            //ofc.UpdateOrderStatus(ddlNos.SelectedValue);
            Orders order = new OrderFacade().FindOrderByID(ddlNos.SelectedValue);
            Supplier supplier = sup.FindBySupplierID(DropDownList1.SelectedValue);
            Employee emp = efc.FindEmployeeByID(empid);
            string emailTo = supplier.Email.Trim();
            string emailFrom = emp.Email.Trim();
            string subject = "Purchase Order From Logic University";
            string body = mailBody(order, supplier, emp);
            sendMail(emailFrom, emailTo, subject,body);
            btnSend.Visible = false;
            lblPurchase.Visible = false;
            ddlNos.Visible = false;
            GridView1.Visible = false;
            lblMsg.Visible = true;
        }

        public string mailBody(Orders order, Supplier supplier, Employee emp)
        {
            string body = body = "<DT>Dear " + supplier.ContactName + ",";
            body += "<p>" + "<DD>" + "This is the purchase order from Logic University.</DD>" + "Purchase Order Number:<b>" + order.PONo;
           //body += "</b> <br>Expected Date: <b>" + order.ExpectedDate.Date.ToString("MMMM dd yyyy") + "</b></p><br><br>";
            body += GridViewToHtml(GridView1) + "<br><br><br>";
            body += "Regards,<br>" + emp.EmployeeName;
            return body;
        }

        public void sendMail(string from, string to,string subject,string body)
        {
            MailMessage m = new MailMessage(from, to);
            m.Subject = subject;
            m.IsBodyHtml = true;
            m.Body = body;
            SmtpClient c = new SmtpClient("lynx.iss.nus.edu.sg");
            c.Send(m);
            c.Dispose();
        }

        private string GridViewToHtml(GridView gv)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gv.RenderControl(hw);
            return sb.ToString();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
        }
    }
}